﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Collections.Generic;

namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class CustomGoodsCheckList : BasePage
    {
        public CustomGoodsCheckList()
        {
            this.ModuleNo = Module.采购项目管理.ToString();
            this.PageOperateType = OperateType.备案采购产品审核.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
             
                //设置下拉框Enable
                GetDefaultAreaString();
                //绑定盟市审核状态
                 ddlAuditStatusMS.Items.Add(new ListItem("全部","0"));
                 ddlAuditStatusMS.Items.Add(new ListItem("未审核","1"));
                 ddlAuditStatusMS.Items.Add(new ListItem("需修改","2"));
                 ddlAuditStatusMS.Items.Add(new ListItem("已通过","3"));
             
               ddlAuditStatusMS.Items.Add(new ListItem("已拒绝", "4"));
                //第一次加载 默认选择
               if (CurrentAccount.GetUser().Org.Area.AreaID.Contains("0000"))
               {

                   labAuditStatusMS.Text = "盟市审核状态";
                   ddlAuditStatusMS.Items[3].Selected = true;//中心默认选择    盟市审核通过的
               }
               else
               {
                   labAuditStatusMS.Text = "审核状态";
                   ddlAuditStatusMS.Items[1].Selected = true;//盟市默认选择    未审核的
               }
                ///绑定数据
               BindGridView();
            }
        }

        /// <summary>
        /// 执行筛选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            //
            AspNetPager1.CurrentPageIndex = 1;
            BindGridView();
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindGridView()
        {
          
            
            int totalCount;
            IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
            IList<HZ.DrugTrade.Model.View.CustomGoods> cstList = cstCtrl.GetCustomGoodsByWhere(GetWhere(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            if (cstList.Count>0)
            {
                btnExport.Visible = true;               
            }
            else
            {
                btnExport.Visible = false;
            }

            this.gvwBidDrug.DataSource = cstList;
            this.gvwBidDrug.DataBind();

           

        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        /// <summary>
        /// 格式化审核状态
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string FormatAuditStatus(object obj)
        {
            string status = "未知";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {
                    case 1: status = "未审核";
                        break;
                    case 2: status = "需修改";
                        break;
                    case 3: status = "已通过";
                        break;
                    case 4: status = "已拒绝";
                        break;
                }
            }
            return status;
        }

        /// <summary>
        /// gridview命令事件响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwBidDrug_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //删除
            if (e.CommandName == "Del")
            {
                string strID = e.CommandArgument.ToString();
                CustomGoods model = new CustomGoods() { ID = Convert.ToInt32(strID) };

                IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
                int flag = cstCtrl.DeleteCustomGoods(model);
                if (flag == 0)
                {
                    HZ.Web.WebUtil.Alert("删除失败！", this);
                }
                if (flag == 1)
                {
                    HZ.Web.WebUtil.Alert("删除成功！", this);

                    //刷新列表
                    BindGridView();
                }
            }
        }


        /// <summary>
        /// 构造查询条件
        /// </summary>
        /// <returns></returns>
        protected HZ.DrugTrade.Model.Query.CustomGoods GetWhere()
        {
            //构造查询条件
            Model.Query.CustomGoods where = new HZ.DrugTrade.Model.Query.CustomGoods();


            if (CurrentAccount.GetUser().Org.Area.AreaID.Contains("0000"))
            {

                //    where.AuditStatusMS = 3;//盟市审核通过

                where.AuditStatus = 1; //未审核的产品
            }
            else
            {
                where.SubmitToUS = 1; //已提交的产品
                where.AddAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
                where.AuditStatusMS = 1; //盟市未审核的产品

            }
            //盟市审核状态
            switch (ddlAuditStatusMS.SelectedValue)
            {
                case "0":
                    where.AuditStatusMS = null;
                    break;
                case "1":
                    where.AuditStatusMS = 1;
                    break;
                case "2":
                    where.AuditStatusMS = 2;
                    break;
                case "3":
                    where.AuditStatusMS = 3;
                    break;
                case "4":
                    where.AuditStatusMS = 4;
                    break;
            }
            where.Outlookc = txtOutLook.Text.Trim();
            where.AreaID = GetAreaID();//地区
            where.PackUnit = txtPackUnit.Text.Trim();
            where.Unit = txtUnit.Text.Trim();
            where.CompanyName = txtProduceCompany_SC.Text.Trim();
            where.GoodsName = txtGoodsName.Text.Trim();
            where.Material = txtMaterial.Text.Trim();
            where.HospitalName = txtHospitalName.Text.Trim();
            where.SortFields = " AddTime Desc";

            return where;
        }

        /// <summary>
        /// 2009-11-19 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {             
            int totalCount;
            IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
            IList<HZ.DrugTrade.Model.View.CustomGoods> cstList = cstCtrl.GetCustomGoodsByWhere(GetWhere(), 1, 0, out totalCount);
            DataTable dt = HZ.Data.ModelConvert.GetDataTableFromModel(cstList);
            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwBidDrug);
        }

        #region 获取地区信息
        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                }

            }
            return areaId;
        }

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "150000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }
        #endregion

    }
}
